﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class People_Edit : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      LoadFields();
    }
  }

  Dictionary<WebControl, string> FieldMappings()
  {
    Dictionary<WebControl, string> fieldMappings = new Dictionary<WebControl, string>();
    fieldMappings[txtSid] = "sid";
    fieldMappings[txtCreated] = "created";
    fieldMappings[txtIp] = "ip_address";
    fieldMappings[txtUid] = "uid";
    fieldMappings[txtUsername] = "username";
    fieldMappings[txtFirstname] = "first_name";
    fieldMappings[txtLastname] = "last_name";
    fieldMappings[txtEmail] = "email";
    fieldMappings[txtCellMob] = "cell_mobile";
    fieldMappings[txtStreet] = "street";
    fieldMappings[txtCity] = "city";
    fieldMappings[txtState] = "state";
    fieldMappings[txtPostcode] = "postcode";
    fieldMappings[txtCountry] = "country";
    fieldMappings[txtHomephone] = "home_phone";
    fieldMappings[txtAge] = "age";
    fieldMappings[txtGender] = "gender";
    fieldMappings[txtNotes] = "notes";
    fieldMappings[ddlAgeGroup] = "age_group";
    fieldMappings[ddlInvolvement] = "involvement";
    return fieldMappings;
  }

  void LoadFields()
  {
    if (HasId())
    {
      litNewEdit.Text = "Edit";

      var dict = Conn.GetRowAsDict("select * from people where id=@id", Id());
      foreach (KeyValuePair<WebControl, string> field in FieldMappings())
      {
        if (field.Key is TextBox)
          ((TextBox)field.Key).Text = Util.ToText(dict[field.Value]);
        if (field.Key is DropDownList)
        {
          ((DropDownList)field.Key).DataBind();
          ((DropDownList)field.Key).SelectedValue = Util.ToText(dict[field.Value]);
        }

      }
    }
    else
    {
      litNewEdit.Text = "New";
    }
  }

  bool HasId()
  {
    int i;
    return int.TryParse(Request.Params["id"], out i);
  }
  int Id()
  {
    if (!HasId()) return -1;
    return int.Parse(Request.Params["id"]);
  }

  protected void bnSave_Click(object sender, EventArgs e)
  {
    Dictionary<string, object> values = new Dictionary<string, object>();
    foreach (KeyValuePair<WebControl, string> field in FieldMappings())
    {
      if (field.Key is TextBox)
        values[field.Value] = Util.ToSqlStr(((TextBox)field.Key).Text);
      if (field.Key is DropDownList)
        values[field.Value] = Util.ToSqlStr(((DropDownList)field.Key).SelectedValue);
    }
    int newId = Conn.UpdateOrInsert("people", Id(), values);
    Response.Redirect("People-Edit.aspx?id=" + newId.ToString());
  }
  protected void bnDelete_Click(object sender, EventArgs e)
  {
    string deleteConfirm = "Confirm deletion";
    if (bnDelete.Text == deleteConfirm)
    {
      Conn.ExecuteNonQueryWithId("delete from people where id=@id", Id());
      Response.Redirect("People.aspx");
    }
    else
    {
      bnDelete.Text = deleteConfirm;
    }
  }
  protected void ddlInvolvement_DataBound(object sender, EventArgs e)
  {
    ((DropDownList)sender).Items.Insert(0, "");
  }
  protected void ddlAgeGroup_DataBound(object sender, EventArgs e)
  {
    ((DropDownList)sender).Items.Insert(0, "");
  }
}
